<?php
    include "library/demo.php";

    $link = null;
    //建立数据库链接
    function con(){
        $host = 'localhost';
        $user = 'root';
        $pass = '';
        global $link;
        //建立连接,选择数据库,并设置编码
        $GLOBALS['link'] = mysql_connect($host, $user, $pass);
        mysql_select_db("sq_featsky", $link);
        mysql_query("set names utf8");
    }
    con();

    //查询有多少个库并选择
    function a1(){
        if (!$link){
            die('Could not connect: ' . mysql_error());
        }else{
            $result = mysql_list_dbs($link);
            var_dump($result);
            echo '<ul>';
             while( $row = mysql_fetch_object($result) ){
                 echo '<li>'.$row->Database.'</li>';
             }
            echo '</ul>';
            //mysql_free_result( $result );
            //mysql_close( $con );
        }
    }

    //查询数据库
    /**
     **mysql_fetch_row(返回一行作为数组)
     **mysql_fetch_array(返回一行作为关联数组)
     **/
    function a2(){
        global $link;
        $res = mysql_query('select * from message');
        while($val= mysql_fetch_array($res)){
            echo "<br/>";
            print_r($val);
        }
    }
    //a2();

    //查询数据
    function a3(){
        $result = mysql_query("SELECT * FROM message where id=5");
        while($row = mysql_fetch_array($result)){
            echo $row['name'] . " " . $row['text'];
            echo "<br/>";
        }
    }
    //a3();

    //基本排序
    function a4(){
        //根据某一字段对行进行排序
        $result = mysql_query("SELECT * FROM message order by headPath");
        while($row = mysql_fetch_array($result)){
            echo $row['id'] . " " . $row['name'] . " " . $row['headPath'] ;
            echo "<br/>";
        }
    }
    //a4();

    //插入数据
    function a5(){
        global $link;
        $name = '李四';
        $time = 18;
        $text = '高三一班';

        $sql = "insert into message(name, time, text) values('$name','$time','$text')";
        mysql_query($sql,$link); //执行插入语句
        $uid = mysql_insert_id();
        echo $uid;
    }
    //a5();

    //更新数据
    function a6(){
        global $link;
        $result = "update message set name = 'upName' where id = 14 and time = '2'";
        echo mysql_query($result,$link);
    }
    //a6();

    //删除数据
    function a7(){
        echo mysql_query("DELETE FROM message WHERE id=15");
    }
    //a7();

    //============================================================
    function a8(){
        global $link;
        //创建数据库
        if (mysql_query("create database demo",$link)){
            echo "Database created";
        }else{
            echo "Error creating database: " . mysql_error();
        }
        //创建表
        $createTable = "create table test
        (
        firstName varchar(15),
        lastName varchar(15),
        age int
        )";
        mysql_query($createTable,$link);
    }
    //a8();

    //分页查询
    function a9(){
        $page = 2;
        $n = 2;
        $m = ($page - 1) * $n;
        $sql = "select * from message limit $m, $n";
        $result = mysql_query($sql);
        //循环获取当前页的数据
        $data = array();
        while ($row = mysql_fetch_assoc($result)) {
        //print_r($row);
            $data[] = $row;
        }
        print_r($data);
    }
    a9();

    //mysql_close($link);
?>






















